package org.lc.cmd.util;

import java.lang.reflect.Field;

public class ReflectUtil {
    public static Field[] getAllFields(Class<?> clazz, boolean withSuper) {
        // 初始化一个数组来存储所有字段
        Field[] declaredFields = clazz.getDeclaredFields();
        if (!withSuper) {
            return declaredFields;
        }

        Field[] inheritedFields = new Field[0];

        // 递归获取超类的字段，直到达到Object类为止
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null) {
            inheritedFields = getAllFields(superClass, true);
        }

        // 合并声明的字段和继承的字段
        Field[] allFields = new Field[declaredFields.length + inheritedFields.length];
        System.arraycopy(declaredFields, 0, allFields, 0, declaredFields.length);
        System.arraycopy(inheritedFields, 0, allFields, declaredFields.length, inheritedFields.length);

        return allFields;
    }
}
